package au.org.htd.youslide;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import org.apache.poi.hslf.model.Picture;
import org.json.JSONArray;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Background extends DbObject {
   @SuppressWarnings("unused")
   private static final Logger log = Logger.getLogger(Background.class
         .getName());

   @Persistent
   private Blob background;

   @Persistent
   private Blob thumbnail;

   @Persistent
   private String name;

   public Background(PersistenceManager pm) {
      super(pm);
   }

   public Background(PersistenceManager pm, String name, byte[] data) {
      super(pm);
      this.name = name;
      setBackgournd(data);
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public byte[] getBackground() {
      return background.getBytes();
   }

   public void setBackgournd(byte[] background) {
      this.background = new Blob(background);
      Image oldImage = ImagesServiceFactory.makeImage(background);
      Transform resize = ImagesServiceFactory.makeResize(200, 150);
      ImagesService imagesService = ImagesServiceFactory.getImagesService();
      Image newImage = imagesService.applyTransform(resize, oldImage);
      this.thumbnail = new Blob(newImage.getImageData());
   }

   public byte[] getThumbnail() {
      return thumbnail.getBytes();
   }

   public static Background upload(String url) throws AppException {
      String name = url.substring(url.lastIndexOf('/') + 1);
      return upload(Util.getBytesFromURL(url), name);
   }

   public static Background upload(byte[] data, String pName) {
      PersistenceManager pm = PMF.getPM();
      Background picture = new Background(pm, pName, data);
      Background bg = picture.save();
      picture.closeConnection();
      return bg;
   }

   @SuppressWarnings("unchecked")
   public static JSONArray getBackgroundIds() throws IOException {
      PersistenceManager pm = PMF.getPM();
      String query = "select from " + Background.class.getName();
      JSONArray ids = new JSONArray();
      for (Background bg : (List<Background>) pm.newQuery(query).execute()) {
         ids.put(bg.getId());
      }
      pm.close();
      return ids;
   }

   public int getPicType() {
      String pic = getName().toLowerCase();
      if (pic.endsWith(".jpg") || pic.endsWith(".jpeg")) {
         return Picture.JPEG;
      } else if (pic.endsWith(".png")) {
         return Picture.PNG;
      }
      return -1;
   }

   @Override
   public Background get(Long id) {
      Background bg = pm.getObjectById(Background.class, id);
      bg.setPm(pm);
      return bg;
   }
}
